const Service = require('../../../core/service/ApiService');

class RecordDao extends Service {

    //搜索服务记录
    async searchRecord(begin, end, svc_item_id, status) {
        if (!svc_item_id) {
            const sql = `select id,service_staff.name as svc_staff_name,aged.name as aged_name,start_time,end_time,svc_item_list,service_record.score,service_record.status
                         from service_record
                         inner join service_staff on service_record.svc_staff_id = service_staff.svc_staff_id
                         inner join aged on service_record.aged_id = aged.aged_id 
                         where start_time between '${begin}' and '${end}'
                         and service_record.status = '${status}'`;
            const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
            return result.length === 0 ? null : result;
        } else {
            const sql = `select id,service_staff.name as svc_staff_name,aged.name as aged_name,start_time,end_time,svc_item_list,service_record.score,service_record.status
                         from service_record
                         inner join service_staff on service_record.svc_staff_id = service_staff.svc_staff_id
                         inner join aged on service_record.aged_id = aged.aged_id 
                         where start_time between '${begin}' and '${end}'
                         and svc_item_list like '%${svc_item_id}%'
                         and service_record.status = '${status}'`;
            const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
            return result.length === 0 ? null : result;
        }
    }

    //统计查询结果数
    async searchAmount(begin, end, svc_item_id, status) {
        const opt = {
            where: {
                start_time: {
                    $gt: begin,
                    $lt: end,
                },
                status: status
            }
        };
        if(svc_item_id){
            opt.where.svc_item_list= `%${svc_item_id},%`
        }
        return this.model.ServiceRecord.count(opt);
    }

    //搜索服务项目列表
    async searchSvcItemList() {
        return this.model.ServiceItem.findAll({
            attributes: ['svc_item_id', 'name'],
            raw: true
        })
    }

    //服务历史查看详情
    async viewDetail(id) {
        const sql = `select id,service_staff.name as svc_staff_name,aged.name as aged_name,start_time,end_time,svc_item_list,service_record.score,text_evalu,voice_evalu,remark
                    from service_record
                    inner join service_staff on service_record.svc_staff_id = service_staff.svc_staff_id
                    inner join aged on service_record.aged_id = aged.aged_id
                    where id = ${id}`;
        const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
        return result.length === 0 ? null : result;
    }

    //服务历史详情编辑
    async editDetail(svc_record_id, text_evalu, remark) {
        return this.model.ServiceRecord.update({
            text_evalu: text_evalu,
            remark: remark
        }, {
            where: {
                id: svc_record_id
            }
        })
    }
}

module.exports = RecordDao;
